package com.tang.subject.application.controller.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.tang.common.entity.PageResult;
import com.tang.common.entity.Result;
import com.tang.subject.application.controller.converter.SubjectAnswerDTOConverter;
import com.tang.subject.application.controller.converter.SubjectInfoDTOConverter;
import com.tang.subject.application.controller.dto.SubjectInfoDTO;
import com.tang.subject.domain.entity.SubjectAnswerBO;
import com.tang.subject.domain.entity.SubjectInfoBO;
import com.tang.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 添加题目（支持单选、多选、判断、简答）
     *
     * @param subjectInfoDTO 待添加的题目信息
     * @return com.tang.subject.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/5 13:54
     **/
    @PostMapping("/add")
    public Result<Boolean> addSubject(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.addSubject.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),
                    "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),
                    "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),
                    "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertDtoToBoList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            Boolean flag = subjectInfoDomainService.addSubjectInfo(subjectInfoBO);
            return Result.ok(flag);
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("SubjectController.addSubject.error:{}", JSON.toJSONString(subjectInfoDTO), e);
            }
            return Result.fail();
        }
    }

    /**
     * 获取题目信息列表
     *
     * @param subjectInfoDTO 查询条件信息
     * @return com.tang.subject.common.entity.Result<com.tang.subject.common.entity.PageResult < com.tang.subject.application.controllers.dto.SubjectInfoDTO>>
     * @author Tang
     * @date 2024/2/5 13:55
     **/
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> querySubjectList(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectList.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO> result = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(result.convert(SubjectInfoDTOConverter.INSTANCE::convertBoToDto));
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("SubjectController.querySubjectList.error:{}", JSON.toJSONString(subjectInfoDTO), e);
            }
            return Result.fail("分页查询失败");
        }
    }

    /**
     * 获取题目具体信息
     *
     * @param subjectInfoDTO 查询条件信息
     * @return com.tang.subject.common.entity.Result<com.tang.subject.common.entity.PageResult < com.tang.subject.application.controllers.dto.SubjectInfoDTO>>
     * @author Tang
     * @date 2024/2/5 13:55
     **/
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO query = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBO result = subjectInfoDomainService.querySubjectInfo(query);
            SubjectInfoDTO infoDTO = SubjectInfoDTOConverter.INSTANCE.convertBoToDto(result);
            return Result.ok(infoDTO);
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("SubjectController.querySubjectInfo.error:{}", JSON.toJSONString(subjectInfoDTO), e);
            }
            return Result.fail();
        }
    }


    /**
     * 题目全文检索接口
     *
     * @param subjectInfoDTO 检索条件
     * @return com.tang.common.entity.Result<com.tang.common.entity.PageResult < com.tang.subject.application.controller.dto.SubjectInfoDTO>>
     * @author Tang
     * @date 2024/2/24 14:49
     **/
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoDTO>> search(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()), "检索关键字不能为空");
            SubjectInfoBO query = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO> infoBOPageResult = subjectInfoDomainService.searchSubjectInfo(query);
            return Result.ok(infoBOPageResult.convert(SubjectInfoDTOConverter.INSTANCE::convertBoToDto));
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("SubjectController.querySubjectInfo.error:{}", JSON.toJSONString(subjectInfoDTO), e);
            }
            return Result.fail();
        }
    }
}
